package leetcode.editor.cn;

//给出两个 非空 的链表用来表示两个非负的整数。其中，它们各自的位数是按照 逆序 的方式存储的，并且它们的每个节点只能存储 一位 数字。 
//
// 如果，我们将这两个数相加起来，则会返回一个新的链表来表示它们的和。 
//
// 您可以假设除了数字 0 之外，这两个数都不会以 0 开头。 
//
// 示例： 
//
// 输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
//输出：7 -> 0 -> 8
//原因：342 + 465 = 807
// 
// Related Topics 链表 数学 
// 👍 5295 👎 0

import java.util.ArrayList;
import java.util.List;

public class P2AddTwoNumbers {
    public static void main(String[] args) {
        int[] nums1 = new int[]{9,9,9,9,9,9,9,9};
        int[] nums2 = new int[]{9,9,9,9};

        ListNode l1 = new P2AddTwoNumbers().new ListNode(0);
        ListNode temp;
        
        temp = l1;
        for (int i = 0; i < nums1.length; i++) {
            temp.next = new P2AddTwoNumbers().new ListNode(nums1[i]);
            temp = temp.next;
        }

        ListNode l2 = new P2AddTwoNumbers().new ListNode(0);
        temp = l2;
        for (int i = 0; i < nums2.length; i++) {
            temp.next = new P2AddTwoNumbers().new ListNode(nums2[i]);
            temp = temp.next;
        }
        
        
        ListNode result = new P2AddTwoNumbers().new Solution().addTwoNumbers(l1.next, l2.next);
        while (result != null) {
            System.out.print(result.val + ",");
            result = result.next;
        }
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
//leetcode submit region begin(Prohibit modification and deletion)
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode first = l1;
            ListNode second = l2;
            ListNode head = new ListNode();
            ListNode pre = head;
            int addNum = 0;
            int sum = 0;
            while (first != null && second != null) {
                sum = first.val + second.val + addNum;
                if (sum >= 10) {
                    sum = sum - 10;
                    addNum = 1;
                } else {
                    addNum = 0;
                }
                pre.next = new ListNode(sum);
                
                
                pre = pre.next;
                first = first.next;
                second = second.next;
            }

            while (first != null) {
                sum = first.val + addNum;
                if (sum >= 10) {
                    sum = sum - 10;
                    pre.next = new ListNode(sum);
                    addNum = 1;
                } else {
                    pre.next = new ListNode(sum);
                    addNum = 0;
                }
                pre = pre.next;
                first = first.next;
            }

            while (second != null) {
                sum = second.val + addNum;
                if (sum >= 10) {
                    sum = sum - 10;
                    pre.next = new ListNode(sum);
                    addNum = 1;
                } else {
                    pre.next = new ListNode(sum);
                    addNum = 0;
                }
                pre = pre.next;
                second = second.next;
            }
            
            if (addNum == 1) {
                pre.next = new ListNode(1);
            }
            return head.next;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)
}


